package com.blog.search;

import com.blog.domain.Favorites;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class SearchService {

	private static volatile boolean isNeedReopen = false;
	private static String indexDir = null;
	private static IndexSearcher searcher = null;
	private static IKAnalyzer analyzer = new IKAnalyzer(true);
	private static Directory directory = null;
	private static IndexReader reader = null;

	static{
		try{
		indexDir = UpdateIndexService.indexDir;
		File file = new File(indexDir);
		if( !file.exists() ){
			file.mkdirs();
		}
		directory = new NIOFSDirectory(file);
		reader = IndexReader.open(directory);
		searcher = new IndexSearcher(reader);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public static void init(String path){
		try{
			indexDir = path;
			File file = new File(indexDir);
			if( !file.exists() ){
				file.mkdirs();
			}
			directory = new NIOFSDirectory(file);
			reader = IndexReader.open(directory);
			searcher = new IndexSearcher(reader);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public static String path(){
		return indexDir;
	}

	public static void needReopen(){
		isNeedReopen = true;
	}

	public static List<Favorites> search(String keyword,int top) throws IOException, ParseException {
		if(isNeedReopen){
			reader.reopen();
			searcher = new IndexSearcher(reader);
			isNeedReopen = false;
		}

		List<Favorites> fs = Lists.newArrayList();
		//QueryParser qp = new QueryParser(Version.LUCENE_34,"content",analyzer);
		//qp.setDefaultOperator(QueryParser.Operator.AND);

		Map<String,Float> boosts = Maps.newHashMap();
		boosts.put("name",1.3f);
		boosts.put("title",1.2f);
		boosts.put("content",1f);

		MultiFieldQueryParser qp = new MultiFieldQueryParser(Version.LUCENE_34,new String[]{"name","title","content"},analyzer,boosts);
		qp.setDefaultOperator(QueryParser.Operator.AND);
		Query query = qp.parse(keyword);
		TopDocs topDocs = searcher.search(query,top);

		for( int i = 0; i < topDocs.scoreDocs.length; i++ ){
			int docId = topDocs.scoreDocs[i].doc;
			Document doc = searcher.doc(docId);
			String name = doc.get("name");
			String url = doc.get("url");
			String content = doc.get("content");
			String title = doc.get("title");
			String ops = doc.get("ops");

			Favorites f = new Favorites();
			f.setFdescription(content);
			f.setFlink(url);
			f.setFname(name);
			f.setAuthor(title);
			f.setOps(ops == null ? 0 : Integer.parseInt(ops));

			fs.add(f);
		}

		return fs;
	}

	public static void main(String[] args) throws IOException, ParseException {

		SearchService.init("E:\\iworkspace\\blog\\src\\main\\webapp\\index");
		List<Favorites> rets = search("????",100);
		for( Favorites f : rets ){
			System.out.println(f.getAuthor());
		}

	}

}
